package cn.xmstore.service.impl;

import cn.xmstore.dao.UserDao;
import cn.xmstore.entity.User;
import cn.xmstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author： Aeolia
 * @Description： TODO 用户业务层接口实现类
 * @Date： 2021/8/20 13:59
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User login(String account, String password) {
        //正则匹配账号判断账号类型（小米id/手机号/邮箱）
        String reg_id="^[0-8]+$";
        String reg_phoneNum="^[1][0-9]{10}$";
        String reg_email="^[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]+$";

        if (account.matches(reg_id)) {
        //账号为小米id，调用对用dao接口
            System.out.println("id");
            return userDao.loginById(Integer.valueOf(account),password);
        }else if (account.matches(reg_phoneNum)){
        //账号为手机号，调用对用dao接口
            System.out.println("手机号");
            return userDao.loginByPhoneNum(account,password);
        }else if (account.matches(reg_email)){
        //账号为邮箱，调用对用dao接口
            System.out.println("邮箱");
            return userDao.loginByEmail(account,password);
        }else {
            return null;
        }
    }

    @Override
    public void registerByPhoneNum(String phoneNum) {
        userDao.addUserByPhoneNum(phoneNum);
    }

    @Override
    public User findAll() {
        return null;
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public User loginById(int id, String password) {
        return userDao.loginById(id,password);
    }


    @Override
    public List<User> findByPhoneNum() {
        return null;
    }

    @Override
    public List<User> findByEmail() {
        return null;
    }
}
